package com.thinksns.sociax.t4.unit;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;

import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * Created by zhouhao on 2017/5/16.
 */

public class MyBlurTransfornation extends BlurTransformation {

    protected Context context;
    private float alpha;
    private BitmapPool mBitmapPool;

    public MyBlurTransfornation(Context context, int radius, int sampling, float alpha) {
        super(context, radius, sampling);
        this.context = context;
        this.alpha = alpha;
        this.mBitmapPool = Glide.get(context).getBitmapPool();
    }

    @Override
    public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
        Bitmap bmp = super.transform(resource, outWidth, outHeight).get();

        int width = bmp.getWidth();
        int height = bmp.getHeight();

        Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAlpha((int) (alpha * 255));

        canvas.drawBitmap(bmp, 0, 0, paint);
        return BitmapResource.obtain(bitmap, mBitmapPool);
    }

}
